package me.abel.auditing.config;

import me.abel.auditing.entity.BaseEntity;
import org.springframework.data.auditing.AuditingHandler;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.context.PersistentEntities;

import java.util.Date;

public class CustomAuditingHandler extends AuditingHandler {

    public CustomAuditingHandler(MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext) {
        super(mappingContext);
    }

    public CustomAuditingHandler(PersistentEntities entities) {
        super(entities);
    }

    @Override
    public Object markCreated(Object source) {
        if (source instanceof BaseEntity) {
            BaseEntity target = (BaseEntity) source;
            target.setCreatedTime(new Date());
            target.setCreateUser("11");
            target.setTenant("1000");
            return target;
        }
        return source;
    }

    @Override
    public Object markModified(Object source) {
        if (source instanceof  BaseEntity) {
            BaseEntity target = (BaseEntity) source;
            target.setLastUpdatedTime(new Date());
            target.setUpdateUser("11");
            return target;
        }
        return source;
    }
}
